#include "pch.h"
#include "color.h"
#define RGBGetB(argb) (LOBYTE(argb))
#define RGBGetG(argb) (LOBYTE(((WORD)(argb)) >> 8))
#define RGBGetR(argb) (LOBYTE((argb) >> 16))
#define RGBGetA(argb) (LOBYTE((argb) >> 24))
#define ARGB(r, g, b, a) ((COLORREF)(RGB(b, g, r) | (a << 24)))

JOUI::UIColor::UIColor() :DxColorObject(0.f, 0.f, 0.f, 0.f)
{
}

JOUI::UIColor::UIColor(const UIColor& ColorObject) : DxColorObject(ColorObject.DxColorObject)
{
}

JOUI::UIColor::UIColor(const D2D1_COLOR_F& ColorObject) : DxColorObject(ColorObject.r, ColorObject.g, ColorObject.b, ColorObject.a)
{
}

JOUI::UIColor::UIColor(const ColorEnum& Color, const float& AlphaValue) : DxColorObject(Color, AlphaValue)
{
}

JOUI::UIColor::UIColor(const float& R, const float& G, const float& B, const float& A) : DxColorObject(R, G, B, A)
{
}
JOUI::UIColor::UIColor(const float& R, const float& G, const float& B) : DxColorObject(R, G, B)
{
}
JOUI::UIColor::UIColor(const int& R, const int& G, const int& B, const int& A) : DxColorObject(R / 255.f, G / 255.f, B / 255.f, A / 255.f)
{
}
JOUI::UIColor::UIColor(const int& R, const int& G, const int& B) : DxColorObject(R / 255.f, G / 255.f, B / 255.f)
{
}
JOUI::UIColor::UIColor(COLORREF rgb)
{
	DxColorObject.r = RGBGetR(rgb) / 255.f;
	DxColorObject.g = RGBGetG(rgb) / 255.f;
	DxColorObject.b = RGBGetB(rgb) / 255.f;
	DxColorObject.a = 1.f;
}
JOUI::UIColor::UIColor(INT rgba)
{
	if (rgba != 0)
	{
		DxColorObject.r = RGBGetR(rgba) / 255.f;
		DxColorObject.g = RGBGetG(rgba) / 255.f;
		DxColorObject.b = RGBGetB(rgba) / 255.f;
		DxColorObject.a = RGBGetA(rgba) / 255.f;
	}
}
void HexStringLowerToUpper(std::wstring& HexStringNumber)
{
	size_t StringLength = HexStringNumber.size();
	for (size_t Count = 0; Count < StringLength; ++Count)
	{
		if (HexStringNumber[Count] >= L'A' && HexStringNumber[Count] <= 'Z')
		{
			HexStringNumber[Count] += 32;
		}
	}
}
short SingleHexLetterToNum(const wchar_t& Hex)
{
	if (Hex >= '0' && Hex <= '9')
	{
		return Hex - L'0';
	}

	return Hex - 87;
}
short HexStringToNum(const std::wstring& HexNumberString)
{
	short FirstCount = SingleHexLetterToNum(HexNumberString[0]) * 16;
	short SecondCount = SingleHexLetterToNum(HexNumberString[1]);

	return FirstCount + SecondCount;
}
JOUI::UIColor::UIColor(LPCWSTR HexString)
{
	std::wstring Hex = HexString;
	HexStringLowerToUpper(Hex);

	auto  RawHexString = Hex.substr(1, wcslen(HexString) - 1);
	short ColorArrary[3] = { 0, 0, 0 };
	for (size_t Count = 2; Count <= RawHexString.size(); Count += 2)
	{
		ColorArrary[Count / 2 - 1] = HexStringToNum(RawHexString.substr(Count - 2, 2));
	}
	DxColorObject.r = ColorArrary[0] / 255.f;
	DxColorObject.g = ColorArrary[1] / 255.f;
	DxColorObject.b = ColorArrary[2] / 255.f;
	DxColorObject.a = 1.f;
}

INT JOUI::UIColor::R(INT r)
{
	if (r != -1)
	{
		DxColorObject.r = r / 255.f;
		return r;
	}
	return (INT)(DxColorObject.r * 255.f);
}

INT JOUI::UIColor::G(INT g)
{
	if (g != -1)
	{
		DxColorObject.g = g / 255.f;
		return g;
	}
	return (INT)(DxColorObject.g * 255.f);
}

INT JOUI::UIColor::B(INT b)
{
	if (b != -1)
	{
		DxColorObject.b = b / 255.f;
		return b;
	}
	return (INT)(DxColorObject.b * 255.f);
}

INT JOUI::UIColor::A(INT a)
{
	if (a != -1)
	{
		DxColorObject.a = a / 255.f;
		return a;
	}
	return (INT)(DxColorObject.a * 255.f);
}

float JOUI::UIColor::GetR() const
{
	return DxColorObject.r;
}

float JOUI::UIColor::GetG() const
{
	return DxColorObject.g;
}

float JOUI::UIColor::GetB() const
{
	return DxColorObject.b;
}

float JOUI::UIColor::GetA() const
{
	return DxColorObject.a;
}

D2D1::ColorF JOUI::UIColor::GetDxObject() const
{
	return DxColorObject;
}

COLORREF JOUI::UIColor::GetRGB()
{
	return RGB(static_cast<BYTE>(DxColorObject.r * 255.0f), static_cast<BYTE>(DxColorObject.g * 255.0f), static_cast<BYTE>(DxColorObject.b * 255.0f));
}

INT JOUI::UIColor::GetARGB()
{
	return ARGB(static_cast<BYTE>(DxColorObject.r * 255.0f), static_cast<BYTE>(DxColorObject.g * 255.0f), static_cast<BYTE>(DxColorObject.b * 255.0f), static_cast<BYTE>(DxColorObject.a * 255.0f));
}

void JOUI::UIColor::SetR(const float& R)
{
	DxColorObject.r = R;
}

void JOUI::UIColor::SetG(const float& G)
{
	DxColorObject.g = G;
}

void JOUI::UIColor::SetB(const float& B)
{
	DxColorObject.b = B;
}

void JOUI::UIColor::SetA(const float& A)
{
	DxColorObject.a = A;
}


void JOUI::UIColor::SetColorLights(float factor)
{
	DxColorObject.r = std::clamp(DxColorObject.r * factor, 0.0f, 1.f);
	DxColorObject.g = std::clamp(DxColorObject.g * factor, 0.0f, 1.f);
	DxColorObject.b = std::clamp(DxColorObject.b * factor, 0.0f, 1.f);
}

bool JOUI::UIColor::empty()
{
	if (DxColorObject.a == 0.0f)
		return true;
	return (DxColorObject.r == 0.0f &&
		DxColorObject.g == 0.0f &&
		DxColorObject.b == 0.0f &&
		DxColorObject.a == 0.0f);
}

bool JOUI::UIColor::isInRange()
{
	return (DxColorObject.r >= 0.f && DxColorObject.r <= 1.f) &&
		(DxColorObject.g >= 0.f && DxColorObject.g <= 1.f) &&
		(DxColorObject.b >= 0.f && DxColorObject.b <= 1.f);
}
